import axios from 'axios'
import qs from 'qs'
import iView from 'iview'

const BASE_URL = process.env.NODE_ENV === 'development' ? '/mock/' : '/'
async function post (url, params = {}) {
  let result = {}
  iView.LoadingBar.start()
  let instance = axios.create({
    headers: {
      'X-Access-Token': sessionStorage.getItem('accessToken'),
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    timeout: 30000
  })
  try {
    result = await instance.post(BASE_URL + url, qs.stringify(params))
  } catch (e) {
    result = data.response || {data: null}
  }
  iView.LoadingBar.finish()
  return result.data
}

export default {
  install:  (Vue) => {
    Vue.prototype.$post = post
  }
}